let app = getApp();
Page({
  data: {
    data: [], //数据
    wxSearchData:'',
    page: 1,//页数
    pages: 0,//总页数
    house_type: 0, //户型
    userInfo: [],
    OptData:null,
    sort:'asc',//排序
    orderby:'danjia',//
    housetype_list: [{
      name: "正序"
    }, {
      name: "倒序"
    }],
    tabTxt: ['排序', '单价', '销量'], //tab文案
    tab: [true, true, true],
  },

  jumpClient(res) {    
    wx.navigateTo({
      url: '/pages/detail/detail?shopId='+res.currentTarget.id
    });
  },
  // 选项卡
  filterTab(e) {
    var data = [true, true, true],
      index = e.currentTarget.dataset.index,
      id = e.currentTarget.id;
    data[index] = !this.data.tab[index];
    this.setData({
      tab: data
    });
   
    if (id !== '排序' &&id !== '正序' &&id !== '倒序'){
      let orderbyId='';
        if (id == '销量') {
          orderbyId = 'XiaoShouShuLiang'
        } else if (id == '单价') {
          orderbyId = 'danjia'
        }
        this.setData({
          page:1,
          orderby: orderbyId
        });
        //传一个search判断是上拉加载还是重新加载
      this.AjaxLoad({ type:'search'});
    }    
  },
  // 获取筛选项
  getFilter() {
    var self = this;
    console.log(self)
  },
  //筛选项点击操作
  filter(e) {
    var self = this,
      id = e.currentTarget.dataset.id,
      txt = e.currentTarget.dataset.txt,
      tabTxt = this.data.tabTxt;
    console.log(e)
    switch (e.currentTarget.id) {
      case '0':
        self.setData({
          page: 1,
          sort:'asc',
          tab: [true, true, true],
        });
        break;
      case '1':
        self.setData({
          page: 1,
          sort: 'desc',
          tab: [true, true, true],
        });
        break;
    }
    //数据筛选
    this.AjaxLoad({ type: 'search' });
  },
  onLoad(opt) {
    wx.showLoading();
    if(opt.name){
      this.setData({
        wxSearchData:opt.name
      });
    }
    this.setData({
       OptData:opt,
    });
    this.AjaxLoad();
  },
  onReachBottom() {
    // 上拉刷新
    let pageNumber = this.data.page,
      pagesNumber = this.data.pages;
    pageNumber++;
    this.setData({
      page: pageNumber
    });
    if ((pageNumber > pagesNumber) && pagesNumber > 0) {
      return app.toast('没有更多数据');
    }
    this.AjaxLoad(pageNumber);
  },
  //文本框输入时拿到值
  wxSearchInput(res){
    this.setData({
      wxSearchData: res.detail.value
    });
  },
  //软键盘搜索事件
  wxSearchConfirm(res){
    this.setData({ page: 1 });
    this.AjaxLoad({
      type: 'search'
    });
  },
  //软键盘搜索事件
  wxSearchBtn(){
    this.setData({page: 1});
    this.AjaxLoad({
      type:'search'
    });
  },
  //清除事件
  clearInput(){
    this.setData({
      wxSearchData:''
    });
  },
  AjaxLoad(obj){
    let that = this,
      objData={},
      action = that.data.OptData.type;
    obj ? obj = obj : obj='';
    //判断是否全部搜索
    objData={
      action: that.data.OptData.type,
      yema: that.data.page,
      keyname: that.data.wxSearchData,
      zhengfan: that.data.sort,
      orderby: that.data.orderby
    };
    if (action != 'getbyor') {
      objData.QuYuiLeiBieID = that.data.OptData.areaId;
    };
    wx.showLoading();
    app.get('GetShangPinNew', objData).then((res) => {
      wx.hideLoading();
      if(obj.type==='search'){
        this.setData({
          userInfo: res.data,
          pages: Math.round((res.zongtiaoshu) / 10)
        });
      }else{
        this.setData({
          userInfo: that.data.userInfo.concat(res.data),
          pages: Math.round((res.zongtiaoshu) / 10)
        });
      }
    });
  }
})